﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using deal_h2ck.DTO;

namespace deal_h2ck
{
    public partial class master : System.Web.UI.MasterPage
    {
        public static CClock clock = new CClock();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CCurrentContext.IsLogged)
                {
                    if (Session["Email"] == null)
                    {
                        CCurrentContext.User = deal_h2ck.DTO.CKhachHangDTO.FromID(CCurrentContext.Email);
                        CCurrentContext.User.InitCart();
                    }
                }
                DisplayLoginPanel();

            }
            LoadLeftMenu();
            LoadSanPhamMoi();
        }
        private void DisplayLoginPanel()
        {
            hlDangNhap.Visible = !CCurrentContext.IsLogged;
            pnLogged.Visible = CCurrentContext.IsLogged;
            if (CCurrentContext.IsLogged)
            {
                lbInfo.Text = string.Format("Xin chào <b>{0}<b>!", CCurrentContext.Email);
                CalculateCartItems();
            }
        }
        public void CalculateCartItems()
        {
            lnkCart.Text = string.Format("Giỏ hàng ({0})", CCurrentContext.Cart.GetNumberOfItems());
        }
        void LoadLeftMenu()
        {
            string sql = "select * from LOAISANPHAM";
            DataTable dt = SqlDataProvider.LoadSQL(sql);
            dlMenuDoc.DataSource = dt;
            dlMenuDoc.DataBind();
        }
        void LoadSanPhamMoi()
        {
            string sql = "select * from SANPHAM where ngayBatDau= (select MAX(ngayBatDau) from SANPHAM)";
            DataTable dt = SqlDataProvider.LoadSQL(sql);
            dlSanPham_n.DataSource = dt;
            dlSanPham_n.DataBind();
        }

        protected void btnLogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();//Huy session khi logout
            Response.Cookies["Email"].Expires = DateTime.Now.AddDays(-1);
            Response.Redirect(Request.Url.ToString());
        }
        
    }
}